	import { banner} from '@/api/index'
	import { home } from '@/api/index'
	import { onLoad, onReachBottom } from "@dcloudio/uni-app"
	import { ref, reactive } from 'vue'
	let carouselList = ref([])
	// 获取轮播图
	function getCarousel(){
		banner.reqBanner().then(res => {
			if(res.code === 1) {
				carouselList.value = res.data
			}
		})
	}
	// 获取分类
	let navList = ref([])
	function getCate(){
		home.reqCate().then(res => {
			if(res.code === 1) {
				navList.value = res.data
			}
		})
	}
	let likePramas = reactive({
		skip: 0,
		limit: 4
	})
	let likeList = ref([])
	// 加状态 more 更多  loading 加载中 loaded 数据没有了
	let lodingType = ref('more')
	function getLikeList() {
		lodingType.value = 'loading'
		likePramas.skip = likeList.value.length
		home.reqLike(likePramas).then(res => {
			if(res.code === 1) {
				let arr = likeList.value
				arr.push(...res.data)
				likeList.value = arr
				if(likeList.value.length >= res.count) {
					lodingType.value = 'loaded'
				}else {
					lodingType.value = 'more'
				}
			}
		})
	}
	function toProduct(item) {
		let cate_id = item._id
		let title = item.name
		let is_top = 1
		uni.navigateTo({
			url: `/pages/product/list?cate_id=${cate_id}&title=${title}&is_top=${is_top}`
		})
	}
	export default function() {
		onLoad(() => {
			getCarousel()
			getCate()
			getLikeList()
		})
		onReachBottom(() => {
			if(lodingType.value === 'loaded' || lodingType.value === 'loading') return
			getLikeList()
		})
		return { carouselList, navList , likeList, lodingType , toProduct}
	}